package com.cloudkinto.common.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimeConvertUtils {

    private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 将时间戳（秒/毫秒）转换为格式化字符串
     */
    public static String formatTimestamp(Long timestamp) {
        if (timestamp == null) return null;
        long ms = isSecondTimestamp(timestamp) ? timestamp * 1000 : timestamp;
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(ms), ZONE).format(FORMATTER);
    }

    /**
     * 判断是否是秒级时间戳（10位）
     */
    private static boolean isSecondTimestamp(Long timestamp) {
        return timestamp < 10_000_000_000L; // 10^10
    }
}
